<link rel="stylesheet" type="text/css" href="css/passwordStrengthMeter.css" />
<script language="javascript" src="js/passwordStrengthMeter.js"></script>
<script language="javascript">
	jQuery(document).ready(function() {
	
	var bpos = "";
	var perc = 0 ;
	var minperc = 0 ;
	$('#user_pass_n').css( {backgroundPosition: "0 0"} );
        $("#anuluj").click(function() {
                $("#user_name").val(".");
                $("#user_pass").val(".");
                $("#user_pass_n").val(".");
                $("#user_pass_nn").val(".");
                $("#anuluj").submit();
              });
        
        $.validator.addMethod("notEqualTo", function(value, element, param) {
        return this.optional(element) || value != $(param).val();
        }, "Nowe hasło musi być inne niż poprzednie");
        
        $("#fancy").validate({
            rules: {
            user_pass: "required",
            user_pass_n: {
            notEqualTo: "#user_pass"
            }
            }
            });
        $("#powt_nowe").hide();
        $("#zaloguj").hide();
	$('#user_pass_nn').keyup(function(){
		$('#result').html(passwordStrength($('#user_pass_n').val(),$('#user_pass_nn').val())) ;
		perc = passwordStrengthPercent($('#user_pass_n').val(),$('#user_pass_nn').val());

		bpos=" $('#colorbar').css( {backgroundPosition: \"0px -" ;
		bpos = bpos + perc + "px";
		bpos = bpos + "\" } );";
		bpos=bpos +" $('#colorbar').css( {width: \"" ;
		bpos = bpos + (perc * 2) + "px";
		bpos = bpos + "\" } );";
		eval(bpos);
	    	$('#percent').html(" " + perc  + "% ");
                if (perc == 100 ) { 
                    $("#zaloguj").show(); 
                    }
                    
                
	              });
	$('#user_pass_n').keyup(function(){
		$('#result').html(passwordStrength($('#user_pass_n').val(),$('#user_pass_nn').val())) ; 
		perc = passwordStrengthPercent($('#user_pass_n').val(),$('#user_pass_nn').val());
		
		bpos=" $('#colorbar').css( {backgroundPosition: \"0px -" ;
		bpos = bpos + perc + "px";
		bpos = bpos + "\" } );";
		bpos=bpos +" $('#colorbar').css( {width: \"" ;
		bpos = bpos + (perc * 2) + "px";
		bpos = bpos + "\" } );";
		eval(bpos);
	    	$('#percent').html(" " + perc  + "% ");
                
                if (perc < 40 ) { 
                    $("#powt_nowe").hide(); 
                    }
                    else { 
                    $("#powt_nowe").show(); 
                    }
		});
	})
	
</script>


<?
$_SESSION['u']=$_POST['user_name'];
$_SESSION['p']=$_POST['user_pass'];
if ($_POST['anuluj']) unset($_SESSION['zmiana_hasla'], $_SESSION['komunikat']);

if ($_POST['user_pass_n']<>'' && ($_POST['user_pass_n'] == $_POST['user_pass_nn'])){
    if ($_POST['user_pass_n']=='.'){
        #unset($_SESSION, $_POST);
    }
    else { 
	$data90=date("Y-m-d", strtotime("+30 days"));
	$sql="update users set haslo = md5('".$_POST['user_pass_n']."'), haslo_do='".$data90."' 
	where nazwa = '".$_SESSION['u']."'";
	#echo $sql;
        mysql_query($sql);
	logg($sql, $_SESSION['u']);
	unset($_SESSION['zmiana_hasla']);
	$_SESSION['komunikat']='<b>Hasło zostało zmienione <br>Proszę się zalogować</b>';
	$_SESSION['p']=$_POST['user_pass_n'];
    }
}


if ($_POST['user_name'] && $_POST['user_pass'] && $_POST['login']){
$sql="select * from users where nazwa = '".$_POST['user_name']."' and haslo = md5('".$_POST['user_pass']."') ";
$result=mysql_query($sql);
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result))
{
if ($row['akt']==0){
$_SESSION['komunikat']='<b>Konto jest nieaktywne !</b>';
#unset($_SESSION);
}
elseif ($row['haslo_do']<>'' && $row['haslo_do']<=date("Y-m-d")){
$_SESSION['komunikat']='<b>Hasło wygasło musi zostać zmienione</b>';
$_SESSION['zmiana_hasla']=5;
}
else {
$_SESSION['uzytk']=$row;
logg($sql,'nieznany');
echo "<meta http-equiv=\"refresh\" content=\"0;url=index.php\">";
}
}
}
}


echo '<table bgcolor="#D9E0E7" width="50%" height="80%" align="center" border="1" cellspacing="0" cellpadding="0">';
echo '<tr valign="center"><td  align="center">';
#echo '<font color=red>Logowanie</font><br>';
echo '<b>'.$_SESSION['komunikat'].'</b>';
	#echo '<table valign="center" border=0><tr>';
	echo '<form action="index.php" method="post" class="fancy" id="fancy">';
	
	echo '<fieldset >';
    echo '<legend>Logowanie</legend>';
    echo '<ul>';
    echo '<div class="style1" id="main">';
    echo '<li>';
    echo '<label for="cnaz">Użytkownik:*</label>';
    echo '<input name="user_name" id="user_name" size="20" class="required" value="'.$_SESSION['u'].'" />';
    echo '</li>';
    echo '</div>';
    echo '<div class="style1" id="main">';
	echo '<li>';
    echo '<label for="cnaz">Hasło:*</label>';
    echo '<input type="password" name="user_pass" id="user_pass" size="20" class="required" value="'.$_SESSION['p'].'" />';
    echo '</li>';
	echo '</div>';
	
	if ($_SESSION['zmiana_hasla']==5){
		echo '<div class="style1" id="main">';
		echo '<li>';
   		 echo '<label for="cnaz">Nowe hasło:*</label>';
   		 echo '<input type="password" name="user_pass_n" id="user_pass_n" size="20" class="required"  />';
  		  echo '</li>';
		echo '<li>';
		echo '<label for="cnaz">Siła hasła:</label>';
		echo '
	<div class="graybar" id="graybar">
	<div class="colorbar" id="colorbar"></div></div>
		<br /><span class="percent" id="percent">
			0%
		</span>
		&nbsp;&nbsp;
		<span class="result" id="result">
			Wpisz nowe hasło
		</span>
		
	</div>';
		 echo '</li>';
		
		#echo '<td><input type="password" name="user_pass_n" class="panel" size=20></td></tr>';
		echo '</div>';
                echo '<div class="powt_nowe" id="powt_nowe">';
		echo '<div class="style1" id="main">';
		echo '<li>';
   		 echo '<label for="cnaz">Powtórz nowe hasło:*</label>';
   		 echo '<input type="password" name="user_pass_nn" id="user_pass_nn" size="20" class="required" />';
  		  echo '</li>';
  		  echo '</div>';
                  echo '</div>';
	
	}
	

	echo '<li>';
        if ($_SESSION['zmiana_hasla'] == 5) echo '<input class="submit" id="zaloguj" name="login" type="submit" value="Zaloguj"/>';
        echo '&nbsp';
        if ($_SESSION['zmiana_hasla'] == 5) echo '<input class="anuluj" id="anuluj" name="anuluj" type="submit" value="Anuluj"/>';
        else echo '<input class="submit" id="zalogujj" name="login" type="submit" value="Zaloguj"/>';
    
    echo '</li>';
    echo '</ul>';
 	echo '</fieldset>';
 	echo '</form>';
	echo '</td></tr></table>';

unset ($_SESSION['log_kom']);

?>